﻿using OSSUtility.Communicate;
using OSSUtility.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ozone.Device
{
    public class DevHeatPump : DevBase
    {



        /// <summary>
        /// 通风橱2 加热泵
        /// </summary>
        /// <param name="CommType"></param>
        /// <param name="FSrvIP"></param>
        /// <param name="FPort"></param>
        public DevHeatPump(int CommType, string FSrvIP, int FPort) : base(CommType, FSrvIP, FPort)
        {

        }
        /// <summary>
        /// 启动加热泵
        /// </summary>
        /// <returns></returns>
        public bool Start()
        {

            //01 05 00 00 FF 00 8C 3A
            bool temperature = false;
            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x05;
                //起始地址
                ushort startAddr = 0x00;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0xFF;
                bytesData[0] = 0x00;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                //01 05 00 00 FF 00 8C 3A
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray(), 1000);
                if (byteRcv != null && byteRcv.Length == 8)
                {
                    temperature = true;
                }
                else
                {
                    temperature = false;
                }
            }
            catch (Exception ex)
            {

                temperature = false;
            }

            return temperature;
        }


        /// <summary>
        /// 停止加热泵
        /// </summary>
        /// <returns></returns>
        public bool Stop()
        {
            bool temperature = false;
            try
            {
                //01 05 00 00 00 00 CD CA
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x05;
                //起始地址
                ushort startAddr = 0x00;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x00;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                //01 05 00 00 00 00 CD CA
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray(), 1000);
                if (byteRcv != null && byteRcv.Length == 8)
                {
                    temperature = true;
                }
                else
                {
                    temperature = false;
                }
            }
            catch (Exception ex)
            {

                temperature = false;
            }

            return temperature;
        }



        /// <summary>
        /// 设置泵流速
        /// </summary>
        /// <returns></returns>
        public bool SetPumpFlow(string Flow)
        {
            bool temperature = false;
            try
            {
                //01 10 00 04 00 02 04 42 20 00 00E7 EE（42 20 00 00为浮点数的40.0）这里是将流速设置为40.0ml/min
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x10;
                //起始地址
                ushort startAddr = 0x04;
                //发送数据

                byte[] bytesDataFlow = BitConverter.GetBytes(float.Parse(Flow));

                byte[] bytesData = new byte[7];
                bytesData[6] = 0x00;
                bytesData[5] = 0x02;
                bytesData[4] = 0x04;
                bytesData[3] = bytesDataFlow[3];
                bytesData[2] = bytesDataFlow[2];
                bytesData[1] = bytesDataFlow[1];
                bytesData[0] = bytesDataFlow[0];
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);

                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray(), 1000);
                if (byteRcv != null && byteRcv.Length == 8)
                {
                    temperature = true;
                }
                else
                {
                    temperature = false;
                }
            }
            catch (Exception ex)
            {

                temperature = false;
            }

            return temperature;
        }




        /// <summary>
        /// 设置泵温度
        /// </summary>
        /// <returns></returns>
        public bool SetPumpHeat(int Type, string Heat)
        {
            bool temperature = false;
            try
            {

                //设置1路温度
                //01 10 00 06 00 02 04 42 A0 00 00 67 DF
                //设置2路温度
                //01 10 00 08 00 02 04 42 8A 00 00 C7 9B
                //设置3路温度
                //01 10 00 0A 00 02 04 42 8A 00 00 46 42
                //设置4路温度
                //01 10 00 0C 00 02 04 42 8A 00 00 C6 68


                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x10;
                //起始地址
                ushort startAddr = 0x00;
                if (Type == 1)
                {
                    startAddr = 0x06;


                }
                else if (Type == 2)
                {
                    startAddr = 0x08;

                }
                else if (Type == 3)
                {
                    startAddr = 0x0A;
                }
                else if (Type == 4)
                {

                    startAddr = 0x0C;

                }
                else
                {
                    return false;

                }


                byte[] bytesDataFlow = BitConverter.GetBytes(float.Parse(Heat));

                byte[] bytesData = new byte[7];
                bytesData[6] = 0x00;
                bytesData[5] = 0x02;
                bytesData[4] = 0x04;
                bytesData[3] = bytesDataFlow[3];
                bytesData[2] = bytesDataFlow[2];
                bytesData[1] = bytesDataFlow[1];
                bytesData[0] = bytesDataFlow[0];
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                //01 05 00 00 00 00 CD CA
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray(), 1000);
                if (byteRcv != null && byteRcv.Length == 8)
                {
                    temperature = true;
                }
                else
                {
                    temperature = false;
                }
            }
            catch (Exception ex)
            {

                temperature = false;
            }

            return temperature;
        }


        /// <summary>
        /// 获取泵流速
        /// </summary>
        /// <returns></returns>
        public string GetPumpFlow()
        {
            bool temperature = false;
            try
            {
                //01 03 00 00 00 02 C4 0B
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x03;
                //起始地址
                ushort startAddr = 0x00;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x02;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray(), 1000);
                if (byteRcv != null && byteRcv.Length == 9)
                {
                    temperature = true;
                }
                else
                {
                    temperature = false;
                }
            }
            catch (Exception ex)
            {

                temperature = false;
            }

            return "";
        }



        /// <summary>
        /// 获取泵温度
        /// </summary>
        /// <returns></returns>
        public string GetPumpHeat(int Type)
        {
            string temperature = string.Empty;
            try
            {
                //获取1路温度
                //01 03 00 0E 00 02 A5 C8

                //获取2路温度
                //01 03 00 10 00 02 C6 CE

                //获取3路温度
                //01 03 00 12 00 02 64 0E

                //获取4路温度
                //01 03 00 14 00 02 84 0F


                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x03;
                //起始地址
                ushort startAddr = 0x00;
                if (Type == 1)
                {
                    startAddr = 0x0E;


                }
                else if (Type == 2)
                {
                    startAddr = 0x10;

                }
                else if (Type == 3)
                {
                    startAddr = 0x12;
                }
                else if (Type == 4)
                {

                    startAddr = 0x14;

                }
                else
                {


                }
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x02;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                //01 05 00 00 00 00 CD CA
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray(), 1000);
                if (byteRcv != null && byteRcv.Length == 9)
                {


                    temperature = ParseTemperatureFromModbus(byteRcv).ToString();
                    //byte[] bytes = new byte[4];
                    //bytes[0] = byteRcv[3];
                    //bytes[1] = byteRcv[4];
                    //bytes[2] = byteRcv[5];
                    //bytes[3] = byteRcv[6];
                    //temperature = HexToFloat(bytes).ToString();
                }
                else
                {
                    temperature = "";
                }
            }
            catch (Exception ex)
            {

                temperature = "";
            }

            return temperature;
        }




        // 16进制字符串转浮点数（小端模式）
        public float HexToFloat(byte[] bytes)
        {
            //byte[] bytes = new byte[4];
            //bytes[0] = 0x00;
            //bytes[1] = 0x00;
            //bytes[2] = 0x48;
            //bytes[3] = 0x42;
            return BitConverter.ToSingle(bytes, 0);
        }




        public static float ParseTemperatureFromModbus(byte[] data)
        {
            // 基本协议校验
            if (data == null || data.Length < 9)
                throw new ArgumentException("Invalid Modbus RTU data length");

            // 检查功能码和字节数 (示例中功能码03，返回04字节)
            if (data[1] != 0x03 || data[2] != 0x04)
                throw new ArgumentException("Unexpected Modbus function code or byte count");

            // 提取温度数据部分 (跳过地址、功能码、字节数)
            byte[] tempBytes = new byte[4];
            Array.Copy(data, 3, tempBytes, 0, 4);

            // 大端序转小端序
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(tempBytes);
            }

            // 转换为浮点数
            float temperature = BitConverter.ToSingle(tempBytes, 0);



            return temperature;
        }
    }
}
